package com.tengju.bff.infrastructure.config;

import com.tengju.bff.infrastructure.shared.MqMessageConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class PaymentRabbitMqConfig {

    @Value("${payment.rabbitmq.host}")
    private String host;
    @Value("${payment.rabbitmq.port}")
    private Integer port;
    @Value("${payment.rabbitmq.username}")
    private String username;
    @Value("${payment.rabbitmq.password}")
    private String password;
    @Value("${payment.rabbitmq.virtual-host}")
    private String vhost;



    @Bean
    public Queue transferRevokeQueue() {
        return new Queue(MqMessageConstants.TRANSFER_REVOKE_QUEUE);
    }


    @Bean
    public DirectExchange transferRevokeExchange() {
        return new DirectExchange(MqMessageConstants.TRANSFER_REVOKE_EXCHANGE);
    }


    @Bean
    public Binding transferRevokeBinding(Queue transferRevokeQueue, DirectExchange transferRevokeExchange) {
        return BindingBuilder.bind(transferRevokeQueue).to(transferRevokeExchange).with(MqMessageConstants.TRANSFER_REVOKE_ROUTING);
    }

    @Bean(name = "paymentConnectionFactory")
    public ConnectionFactory paymentConnectionFactory() {
        CachingConnectionFactory factory = new CachingConnectionFactory();
        factory.setHost(host);
        factory.setPort(port);
        factory.setUsername(username);
        factory.setPassword(password);
        factory.setVirtualHost(vhost);
        return factory;
    }

    @Bean(name = "rabbitListenerContainerFactory")
    public SimpleRabbitListenerContainerFactory paymentListenerFactory(@Qualifier("paymentConnectionFactory") ConnectionFactory paymentConnectionFactory) {
        SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory = new SimpleRabbitListenerContainerFactory();
        simpleRabbitListenerContainerFactory.setConnectionFactory(paymentConnectionFactory);
        simpleRabbitListenerContainerFactory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        return simpleRabbitListenerContainerFactory;
    }


//    @Bean
//    public MessageConverter jacksonMessageConverter() {
//        ObjectMapper objectMapper = new ObjectMapper();
//        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//        return new Jackson2JsonMessageConverter(objectMapper);
//    }
//
//    @Bean
//    public RabbitListenerContainerFactory<?> listenerContainerFactory() {
//        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
//        factory.setMessageConverter(jacksonMessageConverter());
//        return factory;
//    }
//
//    @Bean
//    public RabbitTemplate paymentRabbitTemplate(ConnectionFactory paymentConnectionFactory) {
//        RabbitTemplate template = new RabbitTemplate(paymentConnectionFactory);
//        template.setMandatory(true);
//        template.setMessageConverter(jacksonMessageConverter());
//        return template;
//    }


}
